// content/pages/videoDetail/videoDetail.js
const app = getApp()


Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.path,
    topic: {},
    doctor: '',
    navFlag: 1, //1 简介 2 医生简历
    commentList: [],
    comment: '', //评论内容
    parentId: 0, //回复的评论id
    autoFocus: false,
    resIndex: -1,
  },

  sendHandle() {
    console.log(this.data.comment);


    if (this.data.comment == '') {
      wx.showToast({
        title: '请说点什么',
        icon: 'none'
      })
      return
    }
    var url = app.globalData.url + "main/addComments",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        topicId: that.data.topic.topicId,
        userId: app.globalData.userId,
        commontsContent: that.data.comment,
        parentId: that.data.parentId,
        dateTime: that.getNow(),
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.showToast({
            title: '评论成功',
          })
          that.setData({
            comment: '',
            autoFocus: false,
            parentId: 0,
            resIndex: -1
          })
          wx.hideLoading()
          that.getComment()
        }
      },
      err => {
        console.log(err);
      })
  },

  inputBlur() {
    this.setData({
      resIndex: -1,
      autoFocus: false
    })
  },
  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
  },

  getNow: function () {
    var date = new Date()

    var year = date.getFullYear()
    var month = this.add0(date.getMonth() + 1)
    var day = this.add0(date.getDate())
    var hours = this.add0(date.getHours())
    var min = this.add0(date.getMinutes())
    var se = this.add0(date.getSeconds())
    var str = year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + se
    return str
  },
  add0: function (m) {
    return m < 10 ? '0' + m : m
  },


  resComHandle(e) {
    var id = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.index;
    this.setData({
      parentId: id,
      autoFocus: true,
      resIndex: index
    })
    console.log(this.data);
  },
  // 获取评论
  getComment() {
    var url = app.globalData.url + "main/getTopicCommonts",
      that = this,
      baseUrl = this.data.baseUrl,
      commontList = [];
    wx.showLoading({
      title: '',
    })
    console.log(that.data.topic);
    console.log(that.data.topic.topicId);
    app.wxRequest(url, {
        topicId: that.data.topic.topicId
      }, res => {
        console.log(res);
        var datas = res.data;
        if (res.flag) {
          wx.hideLoading()

          commontList = datas.comments.filter(item => {
            return item.parentId == 0
          })

          console.log(commontList);
          commontList.forEach(ele => {
            ele.userImg = baseUrl + ele.userImg
            ele.childList = []
            datas.comments.forEach(item => {
              if (ele.commontsId == item.parentId) {
                ele.childList.push(item)
              }
            })
          })

          console.log(commontList);
          that.setData({
            commentList: commontList
          })
          console.log(datas);
        }
      },
      err => {
        console.log(err);
      })

  },

  avatarError(e) {
    console.log(e);
    var outIndex = e.currentTarget.dataset.imgindex,
      list = this.data.commentList,
      src = '/img/userImg.jpg';
    list[outIndex].userImg = src
    this.setData({
      commentList: list
    })
  },


  selectNav(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      navFlag: flag
    })

  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('topicDetail', function (data) {
      console.log(data) //传递参数
      that.setData({
        topic: data,
      })
      that.setData({
        doctor: JSON.parse(options.doctor),
      })

      that.getComment()
    });
    console.log(options);



    console.log(this.data);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})